﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace SilverNotes
{
  public class Layers
  {
    public const int Inactive = 0;
    public const int Active = 1;
  }
  public partial class ResizerControl : UserControl
  {
    private bool _isResizing;
    private bool _isDragging;
    private Point _originalMousePos;

    public UIElement ResizeableContent
    {
      get { return vbContent.Child; }
      set { vbContent.Child = value; }
    }

    public ResizerControl()
    {
      InitializeComponent();
      LayoutRoot.MouseLeftButtonDown += OnMouseLeftButtonDown;
      LayoutRoot.MouseLeftButtonUp += OnMouseLeftButtonUp;
      LayoutRoot.MouseMove += OnMouseMove;
      MouseEnter += (o, e) => ShowIndicators(true);
      MouseLeave += (o, e) => ShowIndicators(false);
    }

    private void ShowIndicators(bool show)
    {
      Canvas.SetZIndex(this, show ? Layers.Active : Layers.Inactive);
      if (_isResizing || _isDragging)
      {
        return;
      }
      if (show)
      {
        brdIndicator.BorderBrush = new SolidColorBrush(Colors.Black);
        elSouthEast.Visibility = Visibility.Visible;
      }
      else
      {
        brdIndicator.BorderBrush = new SolidColorBrush(Colors.Transparent);
        elSouthEast.Visibility = Visibility.Collapsed;
      }
    }

    void OnMouseMove(object sender, MouseEventArgs e)
    {
      if (!_isDragging)
      {
        return;
      }
      var pos = e.GetPosition(null);
      var left = Canvas.GetLeft(this) + pos.X - _originalMousePos.X;
      var top = Canvas.GetTop(this) + pos.Y - _originalMousePos.Y;

      Canvas.SetLeft(this, left);
      Canvas.SetTop(this, top);

      _originalMousePos = pos;
    }

    private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
      if(_isDragging)
      {
        LayoutRoot.ReleaseMouseCapture();
        _isDragging = false;
        e.Handled = true;
      }
    }

    private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      _isDragging = true;
      LayoutRoot.CaptureMouse();
      _originalMousePos = e.GetPosition(null);
      e.Handled = true;
    }


    #region // Resizing.

    private void ElSouthEast_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      _isResizing = true;
      elSouthEast.CaptureMouse();
      _originalMousePos = e.GetPosition(null);

      if (double.IsNaN(Width))
      {
        Width = ActualWidth;
      }
      if (double.IsNaN(Height))
      {
        Height = ActualHeight;
      }
      e.Handled = true;
    }

    private void ElSouthEast_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
      if (_isResizing)
      {
        _isResizing = false;
        elSouthEast.ReleaseMouseCapture();
        e.Handled = true;
      }
    }

    private void ElSouthEast_OnMouseMove(object sender, MouseEventArgs e)
    {
      if (!_isResizing)
      {
        return;
      }

      var mousePos = e.GetPosition(null);
      
      var newWidth = Width + mousePos.X - _originalMousePos.X;
      var newHeight= Height + mousePos.Y - _originalMousePos.Y;
      if (newWidth > 0)
      {
        Width = newWidth;
      }
      if (newHeight > 0)
      {
        Height = newHeight;
      }
      _originalMousePos = mousePos;
    }

    #endregion

    private void OnDeleteClick(object sender, EventArgs e)
    {
      ((Panel) Parent).Children.Remove(this);
    }
  }
}